import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.regex.Pattern;

@WebFilter(filterName = "signupFilter", urlPatterns = {"/signup"})
public class signupFilter extends HttpFilter {
    private static final long serialVersionUID = 1L;
    @Override
    protected void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {

        Pattern VALID_EMAIL_ADDRESS_REGEX = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
        if(req.getParameter("passwd").toString().equals(req.getParameter("confirmpasswd").toString()) == false ||
                VALID_EMAIL_ADDRESS_REGEX.matcher(req.getParameter("email")).matches() == false) {
            HttpSession session = req.getSession(true);
            session.setAttribute("error", "signup");
            res.sendRedirect("error.jsp");
        }
	    chain.doFilter(req,res);
    }
}
